package com.example.export.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * excel导出工具类
 * @author yh
 *
 */
@Slf4j
public class ExcelUtils {

	public static InputStream getTemplateStream(String path){
		InputStream template=null;
		try {
			PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
			Resource[] resources= patternResolver.getResources(path);
			if (resources != null && resources.length > 0) {
				template = resources[0].getInputStream();
			}
			return template;
		}catch (Exception e){
			log.error("模版inputstream读取失败",e);
			return null;
		}
	}
	/**
	 * 复制行
	 * 如果是同一个HSSFWorkbook中的行请用此方法
	 */
	public static void copyRows(Workbook workbook, int fromSheetIndex, int toSheetIndex, int startRow, int endRow, int position) {
		Sheet fromSheet = workbook.getSheetAt(fromSheetIndex);
		Sheet toSheet = workbook.getSheetAt(toSheetIndex);
		int i;
		int j;

		if ((startRow == -1) || (endRow == -1)) {
			return;
		}

		List<CellRangeAddress> oldRanges = new ArrayList<>();
		for (i = 0; i < fromSheet.getNumMergedRegions(); i++) {
			oldRanges.add(fromSheet.getMergedRegion(i));
		}

		// 拷贝合并的单元格。原理：复制当前合并单元格后，原位置的格式会移动到新位置，需在原位置生成旧格式
		for (CellRangeAddress oldRange : oldRanges) {
			CellRangeAddress newRange = new CellRangeAddress(oldRange.getFirstRow(), oldRange.getLastRow(),
					oldRange.getFirstColumn(), oldRange.getLastColumn());

			if (oldRange.getFirstRow() >= startRow && oldRange.getLastRow() <= endRow) {
				int targetRowFrom = oldRange.getFirstRow() - startRow + position;
				int targetRowTo = oldRange.getLastRow() - startRow + position;
				oldRange.setFirstRow(targetRowFrom);
				oldRange.setLastRow(targetRowTo);
				toSheet.addMergedRegion(oldRange);
				fromSheet.addMergedRegion(newRange);
			}
		}

		// 设置列宽
		for (i = startRow; i <= endRow; i++) {
			Row fromRow = fromSheet.getRow(i);
			if (fromRow != null) {
				for (j = fromRow.getLastCellNum(); j >= fromRow.getFirstCellNum(); j--) {
					toSheet.setColumnWidth(j, fromSheet.getColumnWidth(j));
					toSheet.setColumnHidden(j, false);
				}
				break;
			}
		}

		// 拷贝行并填充数据
		for (; i <= endRow; i++) {
			Row fromRow = fromSheet.getRow(i);
			if (fromRow == null) {
				continue;
			}
			Row toRow = toSheet.createRow(i - startRow + position);
			toRow.setHeight(fromRow.getHeight());
			for (j = fromRow.getFirstCellNum(); j <= fromRow.getPhysicalNumberOfCells(); j++) {
				Cell fromCell = fromRow.getCell(j);
				if (fromCell == null) {
					continue;
				}
				Cell toCell = toRow.createCell(j);
				toCell.setCellStyle(fromCell.getCellStyle());
				int cType = fromCell.getCellType();
				toCell.setCellType(cType);
				switch (cType) {
					case HSSFCell.CELL_TYPE_BOOLEAN:
						toCell.setCellValue(fromCell.getBooleanCellValue());
						// System.out.println("--------TYPE_BOOLEAN:" +
						// targetCell.getBooleanCellValue());
						break;
					case HSSFCell.CELL_TYPE_ERROR:
						toCell.setCellErrorValue(fromCell.getErrorCellValue());
						// System.out.println("--------TYPE_ERROR:" +
						// targetCell.getErrorCellValue());
						break;
					case HSSFCell.CELL_TYPE_NUMERIC:
						toCell.setCellValue(fromCell.getNumericCellValue());
						// System.out.println("--------TYPE_NUMERIC:" +
						// targetCell.getNumericCellValue());
						break;
					case HSSFCell.CELL_TYPE_STRING:
						toCell.setCellValue(fromCell.getRichStringCellValue());
						// System.out.println("--------TYPE_STRING:" + i +
						// targetCell.getRichStringCellValue());
						break;
				}
			}
		}
	}
}
